package com.hmyd.hmyd_android.ui;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.Base;
import com.hmyd.hmyd_android.utils.MyToast;
import com.hmyd.hmyd_android.utils.Utils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

/**
 * 实名认证
 * 
 * @author wangc
 * 
 */
@ContentView(R.layout.activity_create_account)
public class CreateAccountActivity extends BasicActivity {

	// 标题栏
	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;

	@ViewInject(R.id.realName)
	private EditText realName;
	@ViewInject(R.id.certNo)
	private EditText certNo;
	@ViewInject(R.id.protocol_label)
	private CheckBox protocol_label;
	@ViewInject(R.id.submit)
	private Button mSubmit;

	private final int POSTAUTHREALNAME = 1;

	@ViewInject(R.id.protocal_1)
	private View protocal_1;
	@ViewInject(R.id.protocal_2)
	private View protocal_2;

	@OnClick({ R.id.submit, R.id.protocal_1, R.id.protocal_2 })
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.submit:
			// MyToast.showHMToast(this, true, "开户成功");
			RequestParams params = new RequestParams();
			params.addBodyParameter("realName", realName.getText().toString().trim());
			params.addBodyParameter("certNo", certNo.getText().toString().trim());
			onRequest(POSTAUTHREALNAME, UrlConstant.AUTHREALNAME, HttpMethod.POST, params);

			break;
		case R.id.protocal_1:
			if (AppBasic.app != null && !TextUtils.isEmpty(AppBasic.app.sinaPayAgreementLinkUrl)) {
				Utils.jumpURL(this, AppBasic.app.sinaPayAgreementLinkUrl, "新浪资金托管协议", "", false);
			}
			break;
		case R.id.protocal_2:
			if (AppBasic.app != null
					&& !TextUtils.isEmpty(AppBasic.app.currencyFundAgreementLinkUrl)) {
				Utils.jumpURL(this, AppBasic.app.currencyFundAgreementLinkUrl, "存钱罐服务协议", "", false);
			}
			break;
		default:
			break;
		}

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// initViews();
	}

	// private void initViews() {
	//
	// }

	private TextWatcher watcher = new TextWatcher() {

		@Override
		public void afterTextChanged(Editable s) {
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			submitPatterns();
		}
	};

	private void submitPatterns() {
		if (!TextUtils.isEmpty(realName.getText().toString().trim())
				&& !TextUtils.isEmpty(certNo.getText().toString().trim())
				&& protocol_label.isChecked()) {
			mSubmit.setEnabled(true);
		} else {
			mSubmit.setEnabled(false);
		}
	}

	public void onPostSuccess(int postId, String result) {
		Gson gson = new Gson();
		switch (postId) {
		case POSTAUTHREALNAME:
			java.lang.reflect.Type type2 = new TypeToken<Base>() {
			}.getType();
			Base base = gson.fromJson(result, type2);
			MyToast.showHMToast(this, true, base.info);

			finish();

			break;

		default:
			break;
		}
	}

	@Override
	protected void setParams() {
		home_title_txt.setText("开户");
		home_title.setOnTouchListener(touch);
		realName.addTextChangedListener(watcher);
		certNo.addTextChangedListener(watcher);

		protocol_label.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				submitPatterns();
			}
		});

	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {

	}

	@Override
	public void onPostFailure(int postId, String msg) {

	}
}
